package me.itsoo.artemis.framework.core;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import me.itsoo.artemis.framework.core.tuple.Pair;
import me.itsoo.artemis.framework.core.util.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
 * Environment
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/5/10
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Environment {

    private Map<String, String> cmdParams;

    private Map<String, String> sysVariable;

    private Map<String, String> appProperties;

    /**
     * 创建空 Environment
     *
     * @return Environment
     */
    public static synchronized Environment createEmpty() {
        final Environment result = new Environment();
        if (Objects.isNull(result.cmdParams)) {
            result.cmdParams = new LinkedHashMap<>(32);
        }

        if (Objects.isNull(result.sysVariable)) {
            result.sysVariable = new LinkedHashMap<>(32);
        }

        if (Objects.isNull(result.appProperties)) {
            result.appProperties = new LinkedHashMap<>(32);
        }

        return result;
    }

    /**
     * 创建默认 Environment
     *
     * @return Environment
     */
    public static synchronized Environment createDefault() {
        final Environment result = createEmpty();
        System.getProperties().forEach((k, v) -> result.sysVariable.put(String.valueOf(k), String.valueOf(v)));
        return result;
    }

    /**
     * 创建命令参数 Environment
     *
     * @param cmdParams String[]
     * @return Environment
     */
    public static synchronized Environment createCmdParams(String[] cmdParams) {
        final Environment result = createDefault();
        for (int i = 0, len = (Objects.isNull(cmdParams) ? 0 : cmdParams.length); i < len; i++) {
            if (StringUtils.isNotBlank(cmdParams[i])) {
                final Pair<String, String> pair = StringUtils.split(cmdParams[i], "=");
                result.cmdParams.put(pair.getKey(), pair.getValue());
            }
        }
        return result;
    }

    public static synchronized Environment createAppProperties(String applicationConfig) {
        final Properties appProperties = new Properties();
        new ClassPathFileResource(applicationConfig).loadProperties(appProperties);
        return createAppProperties(appProperties);
    }

    /**
     * 创建应用配置 Environment
     *
     * @param appProperties Properties
     * @return Environment
     */
    public static synchronized Environment createAppProperties(Properties appProperties) {
        final Environment result = createDefault();
        if (Objects.nonNull(appProperties)) {
            appProperties.forEach((k, v) -> result.appProperties.put(String.valueOf(k), String.valueOf(v)));
        }
        return result;
    }

    /**
     * 获取 Environment 的配置
     *
     * @param key String
     * @return String
     */
    public String getProperty(String key) {
        String value = cmdParams.get(key);
        if (Objects.nonNull(value)) {
            return value;
        }

        value = sysVariable.get(key);
        if (Objects.nonNull(value)) {
            return value;
        }

        value = appProperties.get(key);
        if (Objects.nonNull(value)) {
            return value;
        }

        return null;
    }
}
